let accessToken = require('../global').accessToken

const WxCorp = Symbol('Application#wxCorp')

module.exports = {
  get wxCorp() {
    if (!this[WxCorp]) {
      this[WxCorp] = {}
      this[WxCorp].accessToken = accessToken.token
      this[WxCorp].sendMessage = (data) => {
        this.logger.debug(`[egg-wx-corp] sendMessage`, data)

        return this.curl(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${this[WxCorp].accessToken}`, {
          timeout: 20000,
          dataType: 'json',
          contentType: 'json',
          method: 'POST',
          data: data,
        }).then((result) => {
          result = result.data
          this.logger.debug(`[egg-wx-corp] sendMessage result`, result)
        }).catch((err) => {
          this.logger.warn(`[egg-wx-corp] sendMessage err`, err)
          throw err
        })
      }
    }
    return this[WxCorp]
  }
}
